package com.schoolET.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.schoolET.entity.Administrators;
import com.schoolET.entity.dto.AdminListDTO;
import com.schoolET.entity.dto.FileUploadDTO;
import com.schoolET.entity.vo.AdminInsertVO;
import com.schoolET.entity.vo.AdminSelectVO;
import com.schoolET.entity.vo.AdminUpdateVO;
import com.schoolET.error.costom.MyInfoException;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.admin.RegionAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * @author 林
 */
@Api(tags = "区域管理员管理")
@RestController
@RequestMapping("/schoolET")
public class RegionalAdminController {

    @Autowired
    private RegionAdminService regionAdminService;
    //查询所有管理员
    @ApiOperation(value = "查询所用管理员的接口")
    @PostMapping("/admin2List")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result getAdminList(@RequestBody AdminSelectVO adminSelectVO){
        Page<AdminListDTO> adminList = regionAdminService.getAdminList(adminSelectVO);
        return Result.success(MyPage.page(adminList)).msg("查询完成");

    }

    @ApiOperation(value = "添加管理员的接口")
    @PostMapping("/admin2Ins")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result addAdmin(AdminInsertVO adminInsertVO , MultipartFile file) throws IOException {
        Administrators administrators = new Administrators();
        BeanUtil.copyProperties(adminInsertVO,administrators);
        return Result.success(regionAdminService.addAdmin(administrators, file)).msg("添加成功");
    }
    @ApiOperation(value = "删除管理员的接口")
    @PostMapping("/admin2Del/{id}")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result deleteAdmin(@PathVariable Integer id){
        regionAdminService.deleteAdmin(id);
        return Result.success().msg("删除成功");
    }

    @ApiOperation(value = "更新管理员的接口")
    @PostMapping("/admin2Ucg")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result updateAdmin(AdminUpdateVO adminUpdateVO, MultipartFile file) throws IOException {
        if (StpUtil.getLoginIdAsInt() != adminUpdateVO.getId() && !StpUtil.hasRole("admin")) {
            throw new MyInfoException();
        }
        Administrators administrators = new Administrators();
        BeanUtil.copyProperties(adminUpdateVO,administrators);
        FileUploadDTO fileUploadDTO = regionAdminService.updateAdmin(administrators, file);
        return Result.success(fileUploadDTO).msg("修改成功");
    }

    @ApiOperation(value = "二级管理员个人信息")
    @GetMapping("/admin2Information/{id}")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    public Result admin2Information(@PathVariable Integer id) {
        if (StpUtil.getLoginIdAsInt() != id && !StpUtil.hasRole("admin")) {
            throw new MyInfoException();
        }
        return Result.success(regionAdminService.getAdmin(id)).msg("查询成功");
    }
}
